﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ApplicationGenerator.GeneratorMenu.Forms
{
    /// <summary>
    /// Edytor wyświetlający formę do konfiguracji elementu menu
    /// </summary>
    public class UrlSelectEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null)
            {
                if (context.Instance != null)
                {
                    IWindowsFormsEditorService editorService = null;

                    if (provider != null)
                    {
                        editorService =
                            provider.GetService(
                            typeof(IWindowsFormsEditorService))
                            as IWindowsFormsEditorService;
                    }
                    if (editorService != null)
                    {
                        UrlSelectForm urlSelectForm = new UrlSelectForm();
                        if (editorService.ShowDialog(urlSelectForm) == DialogResult.OK)
                        {
                            value = urlSelectForm.SelectedUrl;
                        }
                    }
                }
            }

            return value;
        }
    }
}
